<?php

namespace App\Console\Commands;

use App\Services\CartPriceService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class UpdateCartPrices extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cart:update-prices';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update cart item prices and generate price change notifications';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Starting cart price update...');
        
        $priceService = new CartPriceService();
        $changes = $priceService->batchUpdateAllCartPrices();
        
        $totalCartsWithChanges = count($changes);
        $totalItemsChanged = array_sum(array_map('count', $changes));
        
        $this->info("Cart price update completed.");
        $this->info("Carts with price changes: {$totalCartsWithChanges}");
        $this->info("Total items with price changes: {$totalItemsChanged}");
        
        if ($totalCartsWithChanges > 0) {
            $this->warn("Price changes detected in {$totalCartsWithChanges} carts.");
            $this->info("Users will see price change notifications on their next cart visit.");
        }
        
        return Command::SUCCESS;
    }
}
